﻿using System;
using System.Collections.Generic;
using System.Linq;
using XcoAppSpaces.Contracts.Security;
using System.Net;

namespace XcoAppSpaces.Security.None
{
	public static class CredentialsExtensions
	{
		public static IXcoClientCredentials GetXcoClientCredentials(this ICredentials credentials, string address)
		{
			NetworkCredential cred;
			if (credentials != null && (cred = credentials.GetCredential(AddressToUri(address), XcoBasicClientCredentials.AuthenticationType)) != null)
			{
				return new XcoBasicClientCredentials(cred.UserName, PasswordUtil.CreatePasswordHash(cred.Password));
			}
			return null;
		}

		private static Uri AddressToUri(string address)
		{
			if (address == null)
				return null;
			try
			{
				return new Uri(address);
			}
			catch
			{
				return null;
			}
		}
	}
}
